﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Yz.Base;

namespace Yz.Mvc.Controllers
{
    public class FileController : BaseController
    {
        public FileController(IHostingEnvironment env)
        {
            _env = env;
        }
        /// <summary>
        /// 合并大文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult MergeUploadFile(string fileName, string guid)
        {
            var upFile = MergeFile(fileName, guid);
            return Json(upFile);
        }

        /// <summary>
        /// 文件上传(大文件分片)
        /// </summary>
        /// <param name="chunk"></param>
        /// <param name="chunks"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadBigFile(int? chunk, int? chunks, string guid, IList<IFormFile> files)
        {
            var upFile = new VmUpFile();
            var file = files[0];
            if (chunks == null || chunks == 1)
            {
                upFile = await SaveFile(FileType.Vdo, file);
                upFile.chunk = false;
                return Json(upFile);
            }
            var chu = Convert.ToInt32(chunk);
            upFile = SaveBigFile(chu, guid, file);
            upFile.chunk = true;
            return Json(upFile);
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="ft"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> UploadFile(FileType ft, IList<IFormFile> files)
        {
            var file = files[0];
            var upFile = await SaveFile(ft, file);
            return Json(upFile);
        }

        /// <summary>
        /// 文档预览（pdf）
        /// </summary>
        /// <returns></returns>
        public ActionResult Pdf(string flPath)
        {
            ViewBag.flPath = flPath;
            return View();
        }
        /// <summary>
        /// 视频播放
        /// </summary>
        /// <param name="flImgPath"></param>
        /// <param name="flPath"></param>
        /// <returns></returns>
        public ActionResult Video(string flImgPath, string flPath)
        {
            ViewBag.flPath = flPath;
            ViewBag.flImgPath = flImgPath;
            return View();
        }

    }
}